package com.example.spring.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spring.demo.common.error.CustomException;
import com.example.spring.demo.common.model.req.AssignParam;
import com.example.spring.demo.entity.UserMenu;
import com.example.spring.demo.mapper.UserMenuMapper;
import com.example.spring.demo.service.UserMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserMenuServiceImpl extends ServiceImpl<UserMenuMapper, UserMenu> implements UserMenuService {
    @Override
    @Transactional // 事务
    public void saveMenu(AssignParam param) {
        //先删除，再插入
        //1、删除
        QueryWrapper<UserMenu> query = new QueryWrapper<>();
        query.lambda().eq(UserMenu::getUserId, param.getAssId());
        int delete = baseMapper.delete(query);//删除
        //2、删除成功，再插入
        if (delete > 0) {
            boolean savedMenu = baseMapper.saveMenu(param.getAssId(), param.getList());
            if(!savedMenu) {
                throw new CustomException("保存失败");
            }
        }
    }
}
